#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#
import re


class Enum(object):
    member_re = re.compile(r"^[A-Z][A-Z0-9_]*$")

    def __init__(self):
        self.names = []
        self.values = []
        self.namesdict = {}
        self.valuesdict = {}

    def value_cmp(self, lhs, rhs):
        return cmp(getattr(self, lhs), getattr(self, rhs))

    def getNames(self, modifier=str):
        if not self.names:
            self.names = [modifier(n) for n in sorted(
                [m for m in dir(self) if Enum.member_re.match(m)],
                self.value_cmp)]
        return self.names

    def getValues(self):
        if not self.values:
            self.values = sorted([getattr(self, m) for m in self.getNames()])
        return self.values

    def getNamesDict(self, modifier=str):
        if not self.namesdict:
            self.namesdict = dict(zip(self.getValues(), self.getNames(modifier)))
        return self.namesdict

    def getValuesDict(self, modifier=str):
        if not self.valuesdict:
            self.valuesdict = dict(zip(self.getNames(modifier), self.getValues()))
        return self.valuesdict

    def __getitem__(self, key):
        return self.getNamesDict()[key]


e = Enum()
#e.names = ['A001', 'B002_', 'c002']
e.__setattr__('T', 1)
e.values = [1, 2]
#print dir(e)
print e.getNames()
#print e.getValues()
#print e.getNamesDict()
#print e.getValuesDict()